package ro.prjmanager.helpers;

import ro.prjmanager.database.model.Column;
import ro.prjmanager.database.model.Table;
import ro.prjmanager.model.Columns;
import ro.prjmanager.model.ForeignModel;

import java.util.List;
import java.util.ArrayList;

public class ColumnsColumnHelper {
    private static synchronized void cast(Columns ret, Column col) {
        if (col == null) {
            return;
        }
        ret.setColSize(col.getColumnSize());
        ret.setDatabaseColumnName(col.getDatabaseColumnName());
        ret.setDatabaseType(col.getDatabaseType());

        //getting fk col
        Column colFk = col.getForeingColumn();
        if (colFk != null && colFk.getOwnerTable() != null) {
            ForeignModel fm = new ForeignModel();
            fm.setDatabaseName(colFk.getOwnerTable().getDatabaseName());
            fm.setJavaName(colFk.getOwnerTable().getJavaName());
            Columns colFk_ = cast(colFk);
            colFk_.setFkForeignModel(fm);
            ret.setForeignColumn(colFk_);
        }


        ret.setForeignKey(col.isForeignKey() ? 1 : 0);
        ret.setJavaColumnName(col.getJavaColumnName());
        ret.setNullable(col.isNullable() ? 1 : 0);
        ret.setPrimaryKey(col.isPrimaryKey() ? 1 : 0);
        ret.setJavaType(col.getJavaType());
    }

    private static synchronized void cast(Column ret, Columns col) {
        if (col == null)
            return;
        ret.setColumnSize(col.getColSize());
        ret.setDatabaseColumnName(col.getDatabaseColumnName());
        ret.setDatabaseType(col.getDatabaseType());

        Columns colFk = col.getForeignColumn();
        if (colFk!=null&&col.getFkForeignModel() != null) {

            Column colss = cast(colFk);
            ForeignModel fm = colFk.getFkForeignModel();
            Table table = new Table();
            table.setDatabaseName(fm.getDatabaseName());
            table.setJavaName(fm.getJavaName());
            colss.setOwnerTable(table);
            ret.setForeingColumn(colss);

        }


        ret.setForeignKey(col.getForeignKey() != null && col.getForeignKey() == 1);
        ret.setJavaColumnName(col.getJavaColumnName());
        ret.setNullable(col.getNullable() != null && col.getNullable() == 1);
        ret.setPrimaryKey(col.getPrimaryKey() != null && col.getPrimaryKey() == 1);
        ret.setJavaType(col.getJavaType());
    }


    public static synchronized Columns cast(Column col) {
        if (col == null)
            return null;
        Columns ret = new Columns();
        cast(ret, col);

        List<Column> cols = col.getRelationColumns();
        List<Columns> cols1 = new ArrayList<Columns>();
        if (cols != null && !cols.isEmpty())
            for (Column colmn : cols) {
                Columns col2 = new Columns();
                cast(col2, colmn);
                if(colmn.getOwnerTable()!=null){
                    ForeignModel fm=new ForeignModel();
                    fm.setDatabaseName(colmn.getOwnerTable().getDatabaseName());
                    fm.setJavaName(colmn.getOwnerTable().getJavaName());
                    col2.setFkForeignModel(fm);
                }


                cols1.add(col2);

            }

        ret.setRelationColumns(cols1);
        Columns col1 = new Columns();
        if (col.getForeingColumn() != null) {
            Column colFk=col.getForeingColumn();
            if(colFk!=null&&colFk.getOwnerTable()!=null){
                cast(col1,colFk );
                Table table=colFk.getOwnerTable();
                ForeignModel fm=new ForeignModel();
                fm.setDatabaseName(table.getDatabaseName());
                fm.setJavaName(table.getJavaName());
                col1.setFkForeignModel(fm);
            }
            ret.setForeignColumn(col1);
        }
        return ret;
    }

    public static synchronized Column cast(Columns col) {
        if (col == null)
            return null;
        Column ret = new Column();
        cast(ret, col);

        List<Columns> cols = col.getRelationColumns();
        List<Column> cols1 = new ArrayList<Column>();
        if (cols != null && !cols.isEmpty())
            for (Columns colmn : cols) {
                Column col2 = new Column();
                cast(col2, colmn);
                if(colmn.getFkForeignModel()!=null){
                    Table table=new Table();
                    table.setDatabaseName(colmn.getFkForeignModel().getDatabaseName());
                    table.setJavaName(colmn.getFkForeignModel().getJavaName());
                    col2.setOwnerTable(table);
                }
                cols1.add(col2);

            }

        ret.setRelationColumns(cols1);
        Column col1 = new Column();
        Columns colfk=col.getForeignColumn();
        if(colfk!=null){
            ForeignModel foreignModel=colfk.getFkForeignModel();
            Table table=new Table();
            table.setDatabaseName(foreignModel.getDatabaseName());
            table.setJavaName(foreignModel.getJavaName());
            cast(col1, colfk);
            col1.setOwnerTable(table);
        }
        ret.setForeingColumn(col1);
        return ret;
    }


}
